<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class User extends Model
{
    protected $table = 'user';

    protected $fillable = ['name',  'oauthid'];

    public static function Add_User($User_Info)
    {
        if (count($User_Info) == '0') {
            return false;
        } else {
            $user = self::where('oauthid', '=', $User_Info['oauthid'])->get();
            if (count($user) != '0') {
                self::where('oauthid', '=', $User_Info['oauthid'])->update(['updated_at' => time()]);

                return $user[0]->id;
            } else {
                $user = self::firstOrCreate($User_Info);

                return $user->id;
            }
        }
    }

    public static function Get_Info($Uid)
    {
        return self::find($Uid);
    }

    public function Get_User_Interlocution_List()
    {
         return DB::table('user')->leftJoin('interlocution', 'user.id', '=', 'interlocution.uid')->select(DB::raw(DB::getTablePrefix().'interlocution.`for`,'.DB::getTablePrefix().'user.`id`,'.DB::getTablePrefix().'user.`name`,sum('.DB::getTablePrefix().'interlocution.`status`) as msg'))->groupBy('user.id')->orderBy('msg', 'desc')->paginate(15);
    }

    public function freshTimestamp()
    {
        return time();
    }

    public function fromDateTime($value)
    {
        return $value;
    }

    public function getDateFormat()
    {
        return 'U';
    }
}
